<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class c_users extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->model('m_users');
		if(!isset($_SESSION[SESSION_USERID])){
			redirect(base_url());
		}

	}//end Login

	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : $_GET['task'];
		switch($task){
			case "list":
				$this->users_list();
			break;
			case "create":
				$this->users_create();
			break;
			case "update":
				$this->users_update();
			break;
			case "delete":
				$this->users_delete();
			break;
			case "search":
				$this->users_search();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}

	function index(){
		$data["title"]="users";
		$data["content"]=$this->load->view('admin/v_users_list',$data,TRUE);
		$this->load->view('admin/v_template',$data);
	}

	function users_load(){
		$this->load->view('admin/v_users_list');
	}

	function users_addedit(){
		$data = array();
		$id = $this->uri->segment(3);
		if($id != false){
			$data['dataDetail'] = $this->m_users->getDetail($id);
		}
		$this->load->view('admin/v_users_post', $data);
	}

	function users_list(){
		$query = $this->input->get('query');
		$pagenum = $_GET['pagenum'];
		$pagesize = $_GET['pagesize'];
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;

		$result=$this->m_users->users_list($query, $start, $end);
		echo $result;

	}

	function users_save(){
		$id = $this->input->post('id');
		$group_id = $this->input->post('group_id');
		$group_id = is_numeric($group_id) ? $group_id : 0;
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post('email');
		$fullname = $this->input->post('fullname');
		$handphone = $this->input->post('handphone');
		$task = $this->input->post('task');
		$data = array(
			'username'=>$username,
			'email'=>$email,
			'fullname'=>$fullname,
			'handphone'=>$handphone,
			'user_status'=>'1'
		);
		if($group_id != 0){
			$data["group_id"]=$group_id;
		}
		if($password != ''){
			$data["password"]=md5($password);
		}
		$params = array(
			'id'=>$id,
			'data'=>$data,
			'task'=>$task
		);
		$result = $this->m_users->users_save($params);
		echo $result;
	}



	function users_delete(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : $id;
		$result = $this->m_users->users_delete($id);
		echo $result;
	}

	function users_search(){

	}

	function users_update(){

	}

	function users_create(){

	}

}
?>
